﻿public class NetHandler_Follow : GameServerMessageHandler
{
    public const string Key = "Follow";

    public static NetHandler_Follow NewHandler()
    {
        return new NetHandler_Follow();
    }

    protected override ISerialize NewData() => new FollowRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        FollowRequestData requestData = _data as FollowRequestData;

        GameDebug.Log($"{client.userID} 用户{requestData.fromUserId} follow:{requestData?.toUserId} followOrNot:{requestData.followOrNot}");

        UserServer user = Game.instance.GetUser(client.userID);

        ResponseData response = new ResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                if (requestData.toUserId > 0)
                {
                    if(requestData.followOrNot)
                        user.data.AddMyFollow(requestData.toUserId);
                    else
                        user.data.RemoveMyFollow(requestData.toUserId);
                }
                long fromUserId = requestData.fromUserId;

                long opponent = requestData.toUserId;
                if (opponent > 0)
                {
                    List<long> oppoFollowers = new List<long>();
                    UserServer opponentUser = Game.instance.GetUser(opponent);
                    if (opponentUser != null)
                    {
                        if (requestData.followOrNot)
                            opponentUser.data.AddFollower(fromUserId);
                        else
                            opponentUser.data.RemoveFollower(fromUserId);

                        oppoFollowers = opponentUser.data.followers;
                        users.UpdateFollowers(opponent, oppoFollowers);

                        GameDebug.Log($"给{opponent} 发送关注事件");
                        NetEvent_Follow netEvent = new NetEvent_Follow();
                        netEvent.likeData = new FollowData();
                        netEvent.likeData.followers = user.data.followers;
                        netEvent.SendToRemote(opponentUser.client);
                    }
                    else
                    {
                        users oppouserTable = UserServer.FindInUserTable(opponent);
                        if (oppouserTable != null)
                        {
                            oppoFollowers = users.String2LongList(oppouserTable.followers);
                            if (requestData.followOrNot)
                            {
                                if (!oppoFollowers.Contains(fromUserId))
                                    oppoFollowers.Add(fromUserId);
                            }
                            else
                            {
                                oppoFollowers.Remove(fromUserId);
                            }
                            oppoFollowers.Remove(0);
                            users.UpdateFollowers(opponent, oppoFollowers);
                        }
                    }
                }

                int ret1 = users.UpdateMyFollows(user.data.id, user.data.myFollows);

                if (ret1 >= 0)
                    response.code = 0;
                else
                    response.code = -1;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}
